package com.hzit.ssm.controller;

import com.hzit.ssm.model.BillInfo;
import com.hzit.ssm.resp.Response;
import com.hzit.ssm.service.IUpLoadService;
import com.hzit.ssm.vo.BillExcelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/***
 * 账单上传控制器
 */
@Controller
public class BillUpLoadController {

    @Autowired
    private IUpLoadService upLoadService;


    /**
     * 加载页面
     * @return
     */
    @RequestMapping("/user")
    public String showUser(){
        return "user";
    }


    /**
     * 文件上传
     * @param multipartFile
     * @return
     */
    @ResponseBody
    @RequestMapping("/upload")
    public Response FileUpLoad(@RequestParam(value="file",required=false) MultipartFile multipartFile){

        //定义接口返回对象
        Response response = new Response();

        //获取原文件的文件名
        String extName =  multipartFile.getOriginalFilename();

        //定义文件的存放目录
        String newPath = "D:\\local\\";

        //重命名文件
        String fileName = extName;

        try {
            //文件转移到给定的目标文件。
            File file = new File(newPath + fileName);

            if(file.exists()){
                file.delete();
            }

            multipartFile.transferTo(file);

            int index  = extName.lastIndexOf(".");

            //截取文件后缀
            String suffer  = extName.substring(index,extName.length());

            if (".csv".equals(suffer)){

                //openCsv解析

                upLoadService.csvUpload(newPath + fileName);

            }else if(".xls".equals(suffer) || ".xlsx".equals(suffer)){

                //easyExcel解析excel
                upLoadService.upload(newPath + fileName);

            }else {
                response.setCode(-1);
                response.setMsg("不支持该文件格式");
                return response;
            }


            response.setCode(0);
            response.setMsg("上传成功");

            return response;


        } catch (IOException e) {
            e.printStackTrace();
            response.setCode(-1);
            response.setMsg("网络繁忙，请重试");
            return response;

        }
    }


    /**
     * 查询所有
     * @return
     */

    @ResponseBody
    @RequestMapping("/user/all")
    public Response<List<BillExcelVo>> queryAll(){
        Response <List<BillExcelVo>> response = new Response<List<BillExcelVo>>();


        List<BillExcelVo> billExcelVoList = upLoadService.queryAllBill();

        response.setCode(0);
        response.setMsg("查询成功");
        response.setData(billExcelVoList);

        return response;

    }


    public static void main(String[] args) {

        String extName = "all.csv";

        int index =  extName.lastIndexOf(".");




        System.out.println( extName.substring(index,extName.length()));

    }




}
